package com.ay;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.itextpdf.text.DocumentException;

public class PdfFormUtilsTests
{
	// private static String basePath =
	// "/home/angelo/ws/projects/gitee/itext-testing";

	private static String basePath = "D:/gitee/itext-testing";

	public static void main(String[] args) throws Exception, DocumentException, IOException
	{
		fillCheckbox();
//		 fillTestWorkCard();
	}

	public static void fillCheckbox() throws Exception, DocumentException, IOException
	{
		File file = new File(basePath, "/src/main/resources/test_checkbox.pdf");
		File outFile = new File(basePath, "/src/main/resources/test_checkbox-output.pdf");

		Map<String, Object> pdfFieldMap = PdfFormUtils.getPdfFormFields(new FileInputStream(file));
		Map<String, String> fieldValues = new HashMap<String, String>();
		for (String field : pdfFieldMap.keySet())
		{
			// Off Yes
			fieldValues.put(field, "Yes");
		}

		byte[] data = PdfFormUtils.fillForm(new FileInputStream(file), fieldValues);
		FileOutputStream os = new FileOutputStream(outFile);
		os.write(data);
		os.close();
	}

	public static void fillTestWorkCard() throws Exception, DocumentException, IOException
	{
		File file = new File(basePath, "/src/main/resources/test.pdf");
		File outFile = new File(basePath, "/src/main/resources/test-output.pdf");

		Map<String, Object> pdfFieldMap = PdfFormUtils.getPdfFormFields(new FileInputStream(file));
		Map<String, String> fieldValues = new HashMap<String, String>();
		for (String field : pdfFieldMap.keySet())
		{
			fieldValues.put(field, "Definition: 类型 - " + pdfFieldMap.get(field).toString());
		}

		byte[] data = PdfFormUtils.fillForm(new FileInputStream(file), fieldValues);
		FileOutputStream os = new FileOutputStream(outFile);
		os.write(data);
		os.close();
	}
}
